/*==========================================================================*\
 |  $Id: ServerApplicationSupportStrategy.java 12 2009-11-06 13:45:51Z edwards@cs.vt.edu $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2009 Virginia Tech
 |
 |  This file is part of CloudSpace.
 |
 |  CloudSpace is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  CloudSpace is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU General Public License
 |  along with CloudSpace; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

package student.web.internal;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;

//-------------------------------------------------------------------------
/**
 *  Defines the support methods needed to implement server-specific
 *  storage features for an {@link student.web.Application} that is
 *  running as a plain old Java program, rather than in the context
 *  of a web server application.
 *
 *  @author Stephen Edwards
 *  @author Last changed by $Author: edwards@cs.vt.edu $
 *  @version $Revision: 12 $, $Date: 2009-11-06 13:45:51 +0000 (Fri, 06 Nov 2009) $
 */
public class ServerApplicationSupportStrategy
    implements ApplicationSupportStrategy
{
    // ----------------------------------------------------------
    /**
     *  Get a parameter passed to this page in the query part of the URL.
     *  @param name The name of the parameter to retrieve
     *  @return The parameter's value on the current page, or null if
     *  there is none.
     */
    public String getPageParameter(String name)
    {
        return Executions.getCurrent().getParameter(name);
    }


    // ----------------------------------------------------------
    /**
     *  Get a parameter stored in the current session.
     *  @param name The name of the parameter to retrieve
     *  @return The parameter's value in the current session, or null if
     *  there is none.
     */
    public Object getSessionParameter(String name)
    {
        return Sessions.getCurrent().getAttribute(name);
    }


    // ----------------------------------------------------------
    /**
     *  Remove a parameter stored in the current session, if it exists.
     *  @param name The name of the parameter to remove
     *  @return The removed value, if the parameter existed, or null if
     *  there is no value to remove.
     */
    public Object removeSessionParameter(String name)
    {
        return setSessionParameter(name, null);
    }


    // ----------------------------------------------------------
    /**
     *  Store a value in the current session.  If a value already exists
     *  for the given name, it is replaced.
     *  @param name The name of the parameter to store
     *  @param value The value to store
     *  @return The previous value in the current session associated with
     *  the given name, if there is one, or null otherwise.
     */
    public Object setSessionParameter(String name, Object value)
    {
        Object old = Sessions.getCurrent().getAttribute(name);
        Sessions.getCurrent().setAttribute(name, value);
        return old;
    }


    // ----------------------------------------------------------
    /**
     *  Cause the web application to show a different web page in the
     *  user's web browser.
     *  @param url The new web page to show in the user's browser
     */
    public void showWebPage(String url)
    {
        Executions.sendRedirect(url);
    }


    // ----------------------------------------------------------
    /**
     *  Retrieve the name of the current ZHTML file, such as
     *  "index.zhtml" or "lab02.zhtml".
     *  @return The name of the current ZHTML file, without any
     *  directory component, or "" if there is none.
     */
    public String getCurrentPageName()
    {
        String result = getCurrentPagePath();
        int pos = result.lastIndexOf('/');
        if (pos >= 0)
        {
            result = result.substring(0, pos);
        }
        return result;
    }


    // ----------------------------------------------------------
    /**
     *  Retrieve the relative path name of the current ZHTML file, such
     *  as "/Fall09/mypid/index.zhtml" or "/Fall09/mypid/lab02/lab02.zhtml".
     *  @return The name path to the current ZHTML file,
     *  or "" if there is none.
     */
    public String getCurrentPagePath()
    {
        return Executions.getCurrent().getDesktop().getRequestPath();
    }
}
